package org.lifecycle.config;

import org.lifecycle.model.MyBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LifecycleConfig implements BeanPostProcessor {

    @Bean(initMethod = "初始化方法2", destroyMethod = "销毁方法2")//initMethod的值，是对象中方法的名称
    public MyBean bean(){
        return new MyBean();
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor生命周期：初始化前置方法");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor生命周期：初始化后置方法");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
